﻿#region

using SqlSugar;
using Vin.Tool.Core.JsonCategory;

#endregion

namespace Vin.Extension.SqlSugar.Extension;

public class VinSqlFuncManager
{
    private static SqlFuncExternal AggregateDistinctSum => new()
    {
        UniqueMethodName = "AggregateDistinctSum",
        MethodValue = (expInfo, dbType, context) =>
        {
            if (dbType == DbType.MySql)
            {
                return " SUM(DISTINCT " + expInfo.Args[0].MemberName + ")";
            }

            throw new Exception("未实现");
        }
    };

    private static SqlFuncExternal AggregateDistinctSumNoNull => new()
    {
        UniqueMethodName = "AggregateDistinctSumNoNull",
        MethodValue = (expInfo, dbType, context) =>
        {
            if (dbType == DbType.MySql)
            {
                return " SUM(DISTINCT IFNULL(" + expInfo.Args[0].MemberName + ",0))";
            }

            throw new Exception("未实现");
        }
    };

    private static SqlFuncExternal GroupConcat => new()
    {
        UniqueMethodName = "GroupConcat",
        MethodValue = (expInfo, dbType, context) =>
        {
            if (dbType == DbType.MySql)
            {
                return $" GROUP_CONCAT({expInfo.Args[0].MemberName})";
            }

            throw new Exception("未实现");
        }
    };
    
    private static SqlFuncExternal JsonContains => new()
    {
        UniqueMethodName = "JsonContains",
        MethodValue = (expInfo, dbType, context) =>
        {
            if (dbType == DbType.MySql)
            {
                return $" JSON_CONTAINS({expInfo.Args[0].MemberName}, '{expInfo.Args[1].MemberValue.ToJson()}')";
            }

            throw new Exception("未实现");
        }
    };

    private static SqlFuncExternal JsonExtract => new()
    {
        UniqueMethodName = "JsonExtract",
        MethodValue = (expInfo, dbType, context) =>
        {
            if (dbType == DbType.MySql)
            {
                return $" JSON_EXTRACT({expInfo.Args[0].MemberName}, {expInfo.Args[1].MemberName}) = {expInfo.Args[2].MemberName}";
                // return $"JSON_EXTRACT({expInfo.Args[0].MemberName}, '{expInfo.Args[1].MemberValue.ToJson()}') = {expInfo.Args[2].MemberValue}";
            }

            throw new Exception("未实现");
        }
    };
    
    private static SqlFuncExternal JsonExtractLike => new()
    {
        UniqueMethodName = "JsonExtractLike",
        MethodValue = (expInfo, dbType, context) =>
        {
            if (dbType == DbType.MySql)
            {
                return $" JSON_EXTRACT({expInfo.Args[0].MemberName}, {expInfo.Args[1].MemberName}) like CONCAT('%', {expInfo.Args[2].MemberName}, '%')";
                // return $"JSON_EXTRACT({expInfo.Args[0].MemberName}, '{expInfo.Args[1].MemberValue.ToJson()}') = {expInfo.Args[2].MemberValue}";
            }

            throw new Exception("未实现");
        }
    };


    public static List<SqlFuncExternal> SqlFuncExtensions => new()
    {
        AggregateDistinctSum,
        AggregateDistinctSumNoNull,
        GroupConcat,
        JsonContains,
        JsonExtract,
        JsonExtractLike
    };
}